package com.example.xiaodu.yuejian12.Adapter;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.xiaodu.yuejian12.Activity.MasterDetailActivity;
import com.example.xiaodu.yuejian12.Activity.ShowDetailActivity;
import com.example.xiaodu.yuejian12.Activity.TrainDetailActivity;
import com.example.xiaodu.yuejian12.Bean.MasterBean;
import com.example.xiaodu.yuejian12.Bean.TrainBean;
import com.example.xiaodu.yuejian12.MeIP;
import com.example.xiaodu.yuejian12.MyViewHolder.ItemViewHolder;
import com.example.xiaodu.yuejian12.R;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by xiaodu on 2018/10/17.
 */

public class TrainAdapter extends RecyclerView.Adapter {

    private List<TrainBean> trainBeanList;
    private MeIP ip=new MeIP();
    private String meIP=ip.getIP();

    public void setData(List<TrainBean> list){
        trainBeanList=list;
        notifyDataSetChanged();
    }

    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
        LayoutInflater inflater=LayoutInflater.from(parent.getContext());
        View itemView=inflater.inflate(R.layout.train_item,parent,false);
        ItemViewHolder viewHolder=new ItemViewHolder(itemView);
        return viewHolder;
    }

    public void onBindViewHolder(RecyclerView.ViewHolder holder ,int position){
        final ItemViewHolder viewHolder=(ItemViewHolder)holder;
        TrainBean TrainBean=trainBeanList.get(position);

        String tname=TrainBean.getTname();
        String tintro=TrainBean.getTintro();
        String ttime=TrainBean.getTtime();
        String pic=TrainBean.getTpic();


        viewHolder.tname.setText("训练名称： "+tname);
        viewHolder.tintro.setText("训练简介： "+tintro);
        viewHolder.ttime.setText("训练时间： "+ttime);
        Picasso.with(viewHolder.imageView.getContext()).load(meIP+"/yuejian/public/static/train/"+pic).into(viewHolder.imageView);

        viewHolder.itemView.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v) {
                int pos = viewHolder.getLayoutPosition();
                Intent intent = new Intent(v.getContext(), TrainDetailActivity.class);
                intent.putExtra("trainID", trainBeanList.get(pos).getCid());
                v.getContext().startActivity(intent);
            }
        });



    }

    public  int getItemCount(){
        if (trainBeanList!=null)
            return trainBeanList.size();
        else
            return 0;
    }


}


